// ChimaeraSVN

// Copyright (C) 2008-2009 - ChimaeraSVN

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "StdAfx.h"
#include "SVNPrompt.h"
#include "SVN.h"


SVNPrompt::SVNPrompt()
{
	auth_baton = NULL;
}

SVNPrompt::~SVNPrompt()
{
}

void SVNPrompt::Init(apr_pool_t *pool, svn_client_ctx_t* ctx)
{
	svn_auth_provider_object_t *provider;

	apr_array_header_t *providers = apr_array_make (pool, 4, sizeof (svn_auth_provider_object_t *));

	svn_auth_get_simple_prompt_provider (&provider,
		simpleprompt,
		NULL, /* baton */
		2, /* retry limit */ pool);
	APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;

	svn_auth_get_username_prompt_provider (&provider,
		userprompt,
		NULL, /* baton */
		2, /* retry limit */ pool);
	APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;

	/* Register the auth-providers into the context's auth_baton. */
	svn_auth_open (&auth_baton, providers, pool);
	ctx->auth_baton = auth_baton;
}

svn_error_t* SVNPrompt::userprompt(svn_auth_cred_username_t **cred, void *baton, const char * realm, svn_boolean_t may_save, apr_pool_t *pool)
{
	svn_auth_cred_username_t *ret = (svn_auth_cred_username_t *)apr_pcalloc (pool, sizeof (*ret));

	if (realm)
	{
		printf ("Authentication realm: %s\n", realm);
	}

	char user[10];
	itoa(SVN::recno,user,10);
	ret->username = apr_pstrdup (pool, user);

	*cred = ret;
	return SVN_NO_ERROR;
}

svn_error_t* SVNPrompt::simpleprompt(svn_auth_cred_simple_t **cred, void *baton, const char * realm, const char *username, svn_boolean_t may_save, apr_pool_t *pool)
{
	svn_auth_cred_simple_t *ret = (svn_auth_cred_simple_t *)apr_pcalloc (pool, sizeof (*ret));

	if (realm)
	{
		printf ("Authentication realm: %s\n", realm);
	}

	if (username)
	{
		char user[10];
		itoa(SVN::recno,user,10);
		ret->username = apr_pstrdup (pool, user);
	}
	else
	{
		char user[10];
		itoa(SVN::recno,user,10);
		ret->username = apr_pstrdup (pool, user);
	}

	printf("%s\n",ret->username);

	ret->password = apr_pstrdup (pool, _T("11111"));

	*cred = ret;
	return SVN_NO_ERROR;
}